<!--
 * @Author: malgee
 * @Date: 2025-04-27 14:03:09
 * @LastEditors: maling
 * @LastEditTime: 2025-04-27 15:11:02
 * @FilePath: /vue3-ts-cms/src/base-ui/form/src/mg-form.vue
 * @Description: 
 * 
-->

<template>
  <div class="mg-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :model="modelValue" :label-width="labelWidth">
      <el-row>
        <el-col v-for="item in formItems" :key="item.field" v-bind="colLayout">
          <el-form-item 
            :label="item.label" 
            :rules="item.rules" 
            :style="itemStyle"
          >
            <template v-if="item.type === 'input' || item.type === 'password'">
              <el-input 
                :placeholder="item.placeholder"
                :model-value="modelValue[`${item.field}`]" 
                @update:modelValue="handleValueChange($event, item.field)"
              ></el-input>
            </template>
            <template v-else-if="item.type === 'select'">
              <el-select 
                v-bind="item.otherOptions"
                style="width: 100%;"
                :placeholder="item.placeholder"
                :model-value="modelValue[item.field]" 
                @update:modelValue="handleValueChange($event, item.field)"
              >
                <el-option
                  v-for="option in item.options"
                  :key="option.value"
                  :label="option.title"
                  :value="option.value"
                ></el-option>
              </el-select>
            </template>
            <template v-else-if="item.type === 'detepicker'">
              <el-date-picker
                :model-value="modelValue[item.field]" 
                @update:modelValue="handleValueChange($event, item.field)"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </template>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>
<script setup lang="ts">
import { defineProps, defineEmits } from 'vue'
import { IForm, IFormItem } from '@/base-ui/form/types'
const props = defineProps({
  modelValue: {
    type: Object,
    required: true
  },
  formItems: {
    type: Array as () => IFormItem[],
    default: () => []
  },
  labelWidth: {
    type: String,
    default: '100px'
  },
  colLayout: {
    type: Object,
    default: () => ({
      xl: 6,
      lg: 8,
      md: 12,
      sm: 24,
      xs: 24
    })
  },
  itemStyle: {
    type: Object,
    default: () => ({padding: '10px 40px'})
  }
})

const emit = defineEmits(['update:modelValue'])
const handleValueChange = (value: any, field: string) => {
  // 更新 modelValue  [field]: value 表示动态添加属性
  emit('update:modelValue', {...props.modelValue, [field]: value})  
}
</script>
<style lang="less" scoped>

</style>